#import "FileManager.h"

@implementation FileManager
SYNTHESIZE_SINGLETON_FOR_CLASS(FileManager);

//公用：获取文件路径，如果没有则创建
- (NSString *)getPathInCache:(NSString *)path {
    if (![[NSFileManager defaultManager] fileExistsAtPath:path]) {
        NSError *error;
        [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:NO attributes:nil error:&error];
    }
    return path;
}

//公用：查找文件是否存在
- (BOOL)imageExsit:(NSString *)filename andFolder:(NSString *)folder {
    NSString *filenamePath = [folder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",filename]];
    if ([[NSFileManager defaultManager] fileExistsAtPath:filenamePath]) {
        return YES;
    }else{
        return NO;
    }
}

- (NSString *)cutUrlForImageName:(NSString *)url{
    NSArray *urlArray = [url componentsSeparatedByString:@"/"];
    NSString *name = [NSString stringWithFormat:@"%@",[urlArray objectAtIndex:[urlArray count]-1]];
    return name;
}

#pragma mark -
#pragma makr ---gif图片存放方法---
-(BOOL)saveGif:(NSData *)gifImgData withUrl:(NSString *)url {
    NSString *gifPath = [self getPathInCache:SD_CACHES_FOLDER];
    NSString *gifImageName = [self cutUrlForImageName:url];
    NSError *err = nil;
    NSString *writePath = [gifPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",gifImageName]];
    NSLog(@"gif writepath:%@",writePath);
    if ([gifImgData writeToFile:writePath options:NSAtomicWrite error:&err]) {
        Log(@"success:%@",err);
        return YES;
    }else{
        Log(@"failed:%@",err);
        return NO;
    }
}

#pragma mark -
#pragma mark ---topicDetail图片存放方法---
-(NSString *)saveTopicDetailImg:(NSData *)imageData withUrl:(NSString *)url {
    NSString *topicPath = [self getPathInCache:TOPIC_IMAGE_FOLDER];
    NSString *imageName = [self cutUrlForImageName:url];
    NSString *fileName = [NSString stringWithFormat: @"topic-%0.f-%@",[[NSDate date] timeIntervalSince1970], imageName];
    NSString *writePath = [topicPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",fileName]];
    NSError *err = nil;
    if ([imageData writeToFile:writePath options:NSAtomicWrite error:&err]) {
        Log(@"save success : %@",fileName);
        return fileName;
    }else{
        Log(@"save failed : %@ (%@)",fileName,err);
        return nil;
    }
}

- (BOOL)deleteFile:(NSString *)filename {
    NSString *filenamePath = [TOPIC_IMAGE_FOLDER stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",filename]];
    if ([[NSFileManager defaultManager] fileExistsAtPath:filenamePath]) {
        NSError *err = nil;
        [[NSFileManager defaultManager] removeItemAtPath:filenamePath error:&err];
        Log (@"delete success");
        return YES;
    }else{
        Log (@"delete failed");
        return NO;
    }
}

#pragma mark -
#pragma mark ---welcome图片存放方法---
//扫描欢迎页图片文件夹
- (NSArray *)scanLocalSplashImages{
    NSString *path = [self getPathInCache:SPLASH_IMAGE_FOLDER];
    NSFileManager *fileManage = [NSFileManager defaultManager];
    NSArray *fileNames = [fileManage subpathsOfDirectoryAtPath:path error:nil];
    return fileNames;
}

-(NSString *)saveSplashImage:(NSData *)imageData withUrl:(NSString *)url {
    NSString *topicPath = [self getPathInCache:SPLASH_IMAGE_FOLDER];
    NSString *fileName = [self cutUrlForImageName:url];
    NSString *writePath = [topicPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",fileName]];
    NSError *err = nil;
    if ([imageData writeToFile:writePath options:NSAtomicWrite error:&err]) {
        Log(@"save success : %@",fileName);
        return fileName;
    }else{
        Log(@"save failed : %@ (%@)",fileName,err);
        return nil;
    }
}

- (BOOL)deleteWelcomeFile:(NSString *)filename {
    NSString *filenamePath = [SPLASH_IMAGE_FOLDER stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",filename]];
    if ([[NSFileManager defaultManager] fileExistsAtPath:filenamePath]) {
        NSError *err = nil;
        [[NSFileManager defaultManager] removeItemAtPath:filenamePath error:&err];
        return YES;
    }else{
        return NO;
    }
}

#pragma mark -
#pragma mark ---清除db操作---
- (void)deleteAllFiles {
    NSError *err = nil;
    [[NSFileManager defaultManager] removeItemAtPath:TOPIC_IMAGE_FOLDER error:&err];
}

//日期格式化
//    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//    [dateFormatter setDateFormat:@"MMdd"];
//    NSString *strDate = [dateFormatter stringFromDate:[NSDate date]];


@end
